import 'package:clean_app/asset/color.dart';
import 'package:clean_app/asset/textstyle.dart';
import 'package:clean_app/components/miw.dart';
import 'package:clean_app/provider/tabs_provider.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class AllTabs extends StatefulWidget {
  AllTabs({super.key});

  @override
  State<AllTabs> createState() => _AllTabsState();
}

class _AllTabsState extends State<AllTabs> {
  List<int> level_indexs = [0, 0, 0, 0, 0];
  List<String> picAngle = ["整体", "正面", "左上方", "右上方"];

  List<TabItem> tabs({bool listen = true}) {
    // return [];
    return Provider.of<TabsProvider>(context, listen: listen).tabs;
  }

  List<String> getAllTagTxt() {
    return [
      tabs(listen: false)[level_indexs[0]].inputValue,
      tabs(listen: false)[level_indexs[0]].options![level_indexs[1]].inputValue,
      tabs(listen: false)[level_indexs[0]]
          .options![level_indexs[1]]
          .options![level_indexs[2]]
          .inputValue,
      tabs(listen: false)[level_indexs[0]]
          .options![level_indexs[1]]
          .options![level_indexs[2]]
          .options![level_indexs[3]]
          .inputValue,
      picAngle[level_indexs[4]],
    ];
  }

  pullAllTabs() async {
    var _p = Provider.of<TabsProvider>(context, listen: false);
    _p.pullTabs();
  }

  @override
  void initState() {
    pullAllTabs();
    super.initState();
  }

  Color selectBackgroundColor = os_color;
  Color unselectBackgroundColor = os_color_opa;
  Color selectForegroundColor = os_white;
  Color unselectForegroundColor = os_color;

  TextStyle getTitleTextStyle() {
    return TextStyle(
      fontSize: 20,
      color: os_color,
      fontWeight: FontWeight.bold,
    );
  }

  TextStyle getBtnTextStyle() {
    return TextStyle(
      fontSize: 16,
      fontWeight: FontWeight.bold,
    );
  }

  Widget getDividerLine() {
    return Container();
    return Container(
      margin: EdgeInsets.only(top: 10, left: 5, right: 5),
      height: 1,
      color: os_grey,
    );
  }

  @override
  Widget build(BuildContext context) {
    if (tabs().length == 0) {
      return Center(child: CircularProgressIndicator());
    }
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 20,
        centerTitle: true,
        title: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [],
        ),
        backgroundColor: os_white,
      ),
      backgroundColor: os_white,
      body: Stack(
        children: [
          ListView(
            padding: EdgeInsets.only(left: 10, right: 10, bottom: 10),
            children: [
              Container(
                padding: EdgeInsets.only(left: 10, right: 10, bottom: 10),
                child: Row(
                  children: [
                    Icon(
                      Icons.dashboard_rounded,
                      color: os_color,
                    ),
                    Container(width: 5),
                    Text(
                      "请选择项目",
                      style: getTitleTextStyle(),
                    ),
                  ],
                ),
              ),
              Container(
                child: Wrap(
                  spacing: 10,
                  runSpacing: 5,
                  children: List.generate(tabs().length, (idx) {
                    var ele = tabs()[idx];
                    return ElevatedButton(
                      onPressed: () {
                        setState(() {
                          level_indexs[0] = idx;
                          level_indexs[1] = 0;
                          level_indexs[2] = 0;
                          level_indexs[3] = 0;
                          level_indexs[4] = 0;
                        });
                      },
                      style: ButtonStyle(
                        elevation: WidgetStatePropertyAll(0),
                        backgroundColor: WidgetStateProperty.all(
                          level_indexs[0] == idx
                              ? selectBackgroundColor
                              : unselectBackgroundColor,
                        ),
                        foregroundColor: WidgetStateProperty.all(
                          level_indexs[0] == idx
                              ? selectForegroundColor
                              : unselectForegroundColor,
                        ),
                      ),
                      child: Text(
                        "${idx + 1}. ${ele.inputValue}",
                        style: getBtnTextStyle(),
                      ),
                    );
                  }),
                ),
              ),
              getDividerLine(),
              Container(
                padding: EdgeInsets.all(10),
                margin: EdgeInsets.only(top: 20),
                child: Row(
                  children: [
                    Icon(
                      Icons.fact_check_rounded,
                      color: os_color,
                    ),
                    Container(width: 5),
                    Text(
                      "请选择场景",
                      style: getTitleTextStyle(),
                    ),
                  ],
                ),
              ),
              Container(
                child: Wrap(
                  spacing: 10,
                  runSpacing: 5,
                  children: List.generate(
                      tabs()[level_indexs[0]].options!.length, (idx) {
                    var ele = tabs()[level_indexs[0]].options![idx];
                    return ElevatedButton(
                      onPressed: () {
                        setState(() {
                          level_indexs[1] = idx;
                          level_indexs[2] = 0;
                          level_indexs[3] = 0;
                          level_indexs[4] = 0;
                        });
                      },
                      style: ButtonStyle(
                        elevation: WidgetStatePropertyAll(0),
                        backgroundColor: WidgetStateProperty.all(
                          level_indexs[1] == idx
                              ? selectBackgroundColor
                              : unselectBackgroundColor,
                        ),
                        foregroundColor: WidgetStateProperty.all(
                          level_indexs[1] == idx
                              ? selectForegroundColor
                              : unselectForegroundColor,
                        ),
                      ),
                      child: Text(
                        "${idx + 1}. ${ele.inputValue.toString()}",
                        style: getBtnTextStyle(),
                      ),
                    );
                  }),
                ),
              ),
              // getDividerLine(),
              Container(
                padding: EdgeInsets.all(10),
                margin: EdgeInsets.only(top: 20),
                child: Row(
                  children: [
                    Icon(
                      Icons.tour_rounded,
                      color: os_color,
                    ),
                    Container(width: 5),
                    Text(
                      "请选择设施",
                      style: getTitleTextStyle(),
                    ),
                  ],
                ),
              ),
              Container(
                child: Wrap(
                  spacing: 10,
                  runSpacing: 5,
                  children: List.generate(
                      tabs()[level_indexs[0]]
                          .options![level_indexs[1]]
                          .options!
                          .length, (idx) {
                    var ele = tabs()[level_indexs[0]]
                        .options![level_indexs[1]]
                        .options![idx];
                    return ElevatedButton(
                      onPressed: () {
                        setState(() {
                          level_indexs[2] = idx;
                          level_indexs[3] = 0;
                          level_indexs[4] = 0;
                        });
                      },
                      style: ButtonStyle(
                        elevation: WidgetStatePropertyAll(0),
                        backgroundColor: WidgetStateProperty.all(
                          level_indexs[2] == idx
                              ? selectBackgroundColor
                              : unselectBackgroundColor,
                        ),
                        foregroundColor: WidgetStateProperty.all(
                          level_indexs[2] == idx
                              ? selectForegroundColor
                              : unselectForegroundColor,
                        ),
                      ),
                      child: Text(
                        "${idx + 1}. ${ele.inputValue}",
                        style: getBtnTextStyle(),
                      ),
                    );
                  }),
                ),
              ),
              // getDividerLine(),
              Container(
                padding: EdgeInsets.all(10),
                margin: EdgeInsets.only(top: 20),
                child: Row(
                  children: [
                    Icon(
                      Icons.touch_app_rounded,
                      color: os_color,
                    ),
                    Container(width: 5),
                    Text(
                      "请选择清洁设施",
                      style: getTitleTextStyle(),
                    ),
                  ],
                ),
              ),
              Container(
                child: Wrap(
                  spacing: 10,
                  runSpacing: 5,
                  children: List.generate(
                      tabs()[level_indexs[0]]
                          .options![level_indexs[1]]
                          .options![level_indexs[2]]
                          .options!
                          .length, (idx) {
                    var ele = tabs()[level_indexs[0]]
                        .options![level_indexs[1]]
                        .options![level_indexs[2]]
                        .options![idx];
                    return ElevatedButton(
                      onPressed: () {
                        setState(() {
                          level_indexs[3] = idx;
                          level_indexs[4] = 0;
                        });
                      },
                      style: ButtonStyle(
                        elevation: WidgetStatePropertyAll(0),
                        backgroundColor: WidgetStateProperty.all(
                          level_indexs[3] == idx
                              ? selectBackgroundColor
                              : unselectBackgroundColor,
                        ),
                        foregroundColor: WidgetStateProperty.all(
                          level_indexs[3] == idx
                              ? selectForegroundColor
                              : unselectForegroundColor,
                        ),
                      ),
                      child: Text(
                        "${idx + 1}. ${ele.inputValue}",
                        style: getBtnTextStyle(),
                      ),
                    );
                  }),
                ),
              ),
              // getDividerLine(),
              Container(
                padding: EdgeInsets.all(10),
                margin: EdgeInsets.only(top: 20),
                child: Row(
                  children: [
                    Icon(
                      Icons.wifi_protected_setup_rounded,
                      color: os_color,
                    ),
                    Container(width: 5),
                    Text(
                      "请选择拍摄角度",
                      style: getTitleTextStyle(),
                    ),
                  ],
                ),
              ),
              Container(
                child: Wrap(
                  spacing: 10,
                  runSpacing: 5,
                  children: List.generate(picAngle.length, (idx) {
                    var ele = picAngle[idx];
                    return ElevatedButton(
                      onPressed: () {
                        setState(() {
                          level_indexs[4] = idx;
                        });
                      },
                      style: ButtonStyle(
                        elevation: WidgetStatePropertyAll(0),
                        backgroundColor: WidgetStateProperty.all(
                          level_indexs[4] == idx
                              ? selectBackgroundColor
                              : unselectBackgroundColor,
                        ),
                        foregroundColor: WidgetStateProperty.all(
                          level_indexs[4] == idx
                              ? selectForegroundColor
                              : unselectForegroundColor,
                        ),
                      ),
                      child: Text(
                        "${idx + 1}. ${ele}",
                        style: getBtnTextStyle(),
                      ),
                    );
                  }),
                ),
              ),
              Container(height: 150),
            ],
          ),
          Positioned(
            right: 25,
            bottom: 110,
            child: myInkWell(
              color: os_wonderful_color[3],
              tap: () {
                Navigator.of(context).pushNamed(
                  "/select_pic",
                  arguments: getAllTagTxt(),
                );
              },
              radius: 100,
              widget: Container(
                padding: EdgeInsets.only(
                  left: 20,
                  right: 20,
                  top: 20,
                  bottom: 20,
                ),
                child: Column(
                  spacing: 5,
                  children: [
                    Icon(
                      Icons.add_photo_alternate,
                      color: os_white,
                    ),
                    // Text(
                    //   "前往拍摄",
                    //   style: XSTextStyle(
                    //     context: context,
                    //     color: os_white,
                    //     fontSize: 14,
                    //     fontWeight: FontWeight.bold,
                    //   ),
                    // ),
                  ],
                ),
              ),
            ),
          ),
          Positioned(
            right: 25,
            bottom: 30,
            child: myInkWell(
              color: os_color,
              tap: () {
                Navigator.of(context).pushNamed(
                  "/take_pic",
                  arguments: getAllTagTxt(),
                );
              },
              radius: 100,
              widget: Container(
                padding: EdgeInsets.only(
                  left: 20,
                  right: 20,
                  top: 20,
                  bottom: 20,
                ),
                child: Column(
                  spacing: 5,
                  children: [
                    Icon(
                      Icons.camera_alt,
                      color: os_white,
                    ),
                    // Text(
                    //   "前往拍摄",
                    //   style: XSTextStyle(
                    //     context: context,
                    //     color: os_white,
                    //     fontSize: 14,
                    //     fontWeight: FontWeight.bold,
                    //   ),
                    // ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
